//
//  BDButtonAlert.m
//  BadBoy
//
//  Created by wen on 2017/6/1.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDButtonAlert.h"

@interface BDButtonAlert ()

@property (nonatomic, strong) UIView *bkView;
@property (nonatomic, strong) UIView *contentView;

@end

@implementation BDButtonAlert

+ (void)showWithTitle:(NSString *)title buttonTitleArray:(NSArray *)array finishBlock:(void (^)(NSString *buttonTitle))finishBlock{
    BDButtonAlert *alert = [[BDButtonAlert alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
    alert.backgroundColor = [UIColor clearColor];
    [[UIApplication sharedApplication].keyWindow addSubview:alert];
    
    UIView *cutomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 200)];
    cutomView.backgroundColor= [UIColor whiteColor];
    cutomView.layer.masksToBounds = YES;
    cutomView.layer.cornerRadius = 4;
    
    UIView *bkView = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
    cutomView.center = bkView.center;
    [bkView addSubview:cutomView];
    cutomView.alpha = 0;
    bkView.backgroundColor = [UIColor clearColor];
    [alert addSubview:bkView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [bkView addGestureRecognizer:tap];
    [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        [UIView animateWithDuration:0.25 animations:^{
            bkView.backgroundColor = [UIColor clearColor];
            cutomView.alpha = 0;
        } completion:^(BOOL finished) {
            [alert removeFromSuperview];
        }];
    }];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 270, 40)];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:17];
    titleLabel.textColor = [UIColor colorWithHexString:@"#4d7aad"];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [cutomView addSubview:titleLabel];
    
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeButton setBackgroundImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    closeButton.size = CGSizeMake(15, 15);
    closeButton.right = cutomView.width - 10;
    closeButton.centerY = titleLabel.centerY;
    [cutomView addSubview:closeButton];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 270, 1)];
    line.top = titleLabel.bottom;
    line.backgroundColor = kBDColorGray;
    [cutomView addSubview:line];
    
    CGFloat lastBottom = 0;
    CGFloat buttonWidth = (cutomView.width - 30 ) / 2.0;
    CGFloat buttonHeight = 38;
    for (int i = 0; i < array.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(10 + (buttonWidth + 10) * (i % 2), line.bottom + 10 + (buttonHeight + 10) * (i / 2), buttonWidth, buttonHeight);
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"#4c4c4c"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (finishBlock) {
                finishBlock(array[i]);
            }
//            self.type = buttonArr[i];
//            [self requestDatasource];
//            [self.tableView reloadData];
            [UIView animateWithDuration:0.25 animations:^{
                bkView.backgroundColor = [UIColor clearColor];
                cutomView.alpha = 0;
            } completion:^(BOOL finished) {
                [alert removeFromSuperview];
            }];
        }];
        button.layer.borderWidth = 1;
        button.layer.borderColor =kBDColorGray.CGColor;
        [cutomView addSubview:button];
        lastBottom = button.bottom;
    }
    
    cutomView.height = lastBottom + 10;
    cutomView.center = bkView.center;
    
    [UIView animateWithDuration:0.25 animations:^{
        bkView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        cutomView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
    
    [[closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UIView animateWithDuration:0.25 animations:^{
            bkView.backgroundColor = [UIColor clearColor];
            cutomView.alpha = 0;
        } completion:^(BOOL finished) {
            [alert removeFromSuperview];
        }];
    }];
}

+ (instancetype)showInShouyeWithView:(UIView *)view buttonTitleArray:(NSArray *)array finishBlock:(void (^)(NSString *buttonTitle))finishBlock{
    BDButtonAlert *alert = [[BDButtonAlert alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
    alert.backgroundColor = [UIColor clearColor];
    
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] init];
    [[tapGR rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        [alert hidden];
    }];
    [alert addGestureRecognizer:tapGR];
    
    alert.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, kBDTopHeight, kBDScreenWidth, 0)];
    alert.contentView.backgroundColor = [UIColor whiteColor];
    alert.contentView.clipsToBounds = YES;
    [alert addSubview:alert.contentView];
    
    CGFloat lastBottom = 0;
    CGFloat buttonWidth = (kBDScreenWidth - 40 ) / 3.0;
    CGFloat buttonHeight = 38;
    
    for (int i = 0; i < array.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(10 + (buttonWidth + 10) * (i % 3), 10 + (buttonHeight + 10) * (i / 3), buttonWidth, buttonHeight);
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:@"#4c4c4c"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (finishBlock) {
                finishBlock(array[i]);
            }
            [alert hidden];
        }];
        button.layer.borderWidth = 1;
        button.layer.borderColor =kBDColorGray.CGColor;
        [alert.contentView addSubview:button];
        lastBottom = button.bottom;
    }
    
    [view addSubview:alert];
    
    [UIView animateWithDuration:0.25 animations:^{
        alert.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        alert.contentView.height = lastBottom + 10;
    } completion:^(BOOL finished) {
        
    }];
    return alert;
}

- (void)hidden{
    [UIView animateWithDuration:0.25 animations:^{
        self.backgroundColor = [UIColor clearColor];
        self.contentView.height = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

+ (NSInteger)getTypeidWitTypeString:(NSString *)typeString{
    if ([typeString isEqualToString:ASLocalizedString(@"水疗")]) {
        return 1;
    }else if ([typeString isEqualToString:@"KTV"]) {
        return 2;
    }else if ([typeString isEqualToString:ASLocalizedString(@"夜总会")]) {
        return 3;
    }else if ([typeString isEqualToString:ASLocalizedString(@"酒吧")]) {
        return 4;
    }else if ([typeString isEqualToString:ASLocalizedString(@"沐足")]) {
        return 5;
    }else if ([typeString isEqualToString:ASLocalizedString(@"健身")]) {
        return 6;
    }else if ([typeString isEqualToString:ASLocalizedString(@"台球")]) {
        return 7;
    }else if ([typeString isEqualToString:ASLocalizedString(@"桌游")]) {
        return 9;
    }else if ([typeString isEqualToString:ASLocalizedString(@"棋牌")]) {
        return 11;
    }else if ([typeString isEqualToString:ASLocalizedString(@"其他")]) {
        return 12;
    }
    return 0;
}

@end
